/*
AlliedBridge Content Management System
Copyright (C) 2006 AlliedBridge

This program is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software Foundation;

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.volume4.alliedbridge.model.media;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

public class DeleteFile extends HttpServlet {

	public String DRIVER, URL, USER, PASS; 
        
        public void init() throws ServletException {
        ServletContext context = getServletContext();
        DRIVER = context.getInitParameter("driver");
        URL = context.getInitParameter("dburl");
        USER = context.getInitParameter("dbuser");
        PASS = context.getInitParameter("dbpass");
        }


	public void doGet (HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
	response.setContentType("text/html");
	PrintWriter out = response.getWriter();
	Connection con = null;

	try {	                
                Class.forName(DRIVER);
		con = DriverManager.getConnection(URL,USER,PASS);		
		// Retreive file information based on fileID
                // from the databse.
                PreparedStatement pstmt = con.prepareStatement(
                "SELECT * FROM fileexchange " +
                "WHERE fileID = '" +  request.getParameter("id") + "'"
                );
                
                ResultSet rs = pstmt.executeQuery();
                
                boolean hasData = false;
                
                while(rs.next()) {
                hasData = true;
                
                String fileID = rs.getString(1);                
                String filePath = rs.getString(3);                
                
                System.out.println(fileID);
                System.out.println(filePath);
                
                // Delete the file from the server
                File selectedFile = new File(filePath);
                System.out.println(selectedFile);
                selectedFile.delete();
                
                //Delete reference to file from database
                Statement stmt = con.createStatement();
                String query = "DELETE FROM fileexchange WHERE fileID = " +
                "'" + fileID + "'";          
              
                int result = stmt.executeUpdate(query);
                response.sendRedirect("file_removed.jsp");
                }
          if(hasData == false) 
          response.sendRedirect("noSuchFile.jsp");
        
} catch (Exception e) {
	e.printStackTrace();
}

finally {
	if (con != null) {
		try {                    
                    con.close();
		}
		catch (SQLException ignore) {}
            }   
        }        
    }
    public void doPost(HttpServletRequest request,
                                HttpServletResponse response)
          throws ServletException, IOException {
              doGet(request, response);
        }
}